﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AddIn.EA.Core.Interface;
using AddIn.EA.Core.Common;
using AddIn.EA.Framework;
using AddIn.EA.Framework.Common;
using AddIn.EA.Architecture.ComponentLogic;
using AddIn.EA.Architecture.Presentation.Interfaces;

namespace AddIn.EA.Architecture.Presentation
{

    public class BaseAddNewEntity : BaseAddInArchitecture, IAddNewBusinessEntity
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        private AddIn.EA.Architecture.Presentation.Controls.BEMain ucMain;

        private BusinessEntity _entity = null;

        #region Constructor

        private BaseAddNewEntity()
        {
            //
        }

        public BaseAddNewEntity(string stereotype)
        {
            this.Stereotype = stereotype;
            this.LoadAddInControls += new EventHandler(AddNewBusinessEntity_LoadAddInControls);
            this.UnloadAddInControls += new EventHandler(AddNewBusinessEntity_UnloadAddInControls);
        }

        void AddNewBusinessEntity_UnloadAddInControls(object sender, EventArgs e)
        {
            this.Controls.Clear();
            this.ucMain.Dispose();
            this.ucMain = null;
            this.Entity = null;
        }

        void AddNewBusinessEntity_LoadAddInControls(object sender, EventArgs e)
        {
            this.ucMain = new AddIn.EA.Architecture.Presentation.Controls.BEMain();

            this.ucMain.Dock = System.Windows.Forms.DockStyle.Fill;
            this.ucMain.Entity = null;
            this.ucMain.Location = new System.Drawing.Point(0, 0);
            this.ucMain.Name = "ucMain";
            this.ucMain.TabIndex = 0;
            this.ucMain.UpdateEA += new System.Action(ucMain_UpdateEA);

            this.Controls.Add(this.ucMain);
            this.ClientSize = new System.Drawing.Size(831, 393);

            this.Entity = new BusinessEntity();
            this.Entity.Stereotype = this.Stereotype;
        }

        #endregion

        #region IAddNewBusinessEntity Members
        public BusinessEntity Entity 
        {
            get { return _entity; }
            set { _entity = value; }
        }

        public void RefreshEntity()
        {
            ucMain.RefreshEntity();
        }

        private string Stereotype { get; set; }

        #endregion

        private void ucMain_UpdateEA()
        {
            this.Close();
        }

        protected override bool UpdateEA(RepositoryManager repository)
        {
            bool updated = base.UpdateEA(repository);

            if (!updated && repository != null)
            {
                IElement selectedObject = repository.GetSelectedObject();

                Package bePackage;
                if (!(selectedObject is Package) || selectedObject.Name != this.Stereotype)
                    bePackage = repository.GetPackageByName(this.Stereotype);
                else
                    bePackage = (Package)selectedObject;

                this.Entity.Update(bePackage);

                updated = true;
            }

            return updated;
        }

        protected override bool CheckShowForm(IElement selectedElement, MenuLocation location)
        {
            return (selectedElement is Package && ((Package)selectedElement).Name == this.Stereotype);
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // AddNewBusinessEntity
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Name = "AddNewBusinessEntity";
            this.ResumeLayout(false);
        }

        #endregion

        
    }
}
